<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\IndustryCate as IndustryCateModel;

class IndustryCateService
{
    /**
     * 获取行业分类
     * @param $pid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getIndustryCate($pid = 0)
    {
        $data = IndustryCateModel::getIndustryCate(['pid' => $pid]);
        foreach ($data as $k => $v) {
            // 一级分类时
            if (!$pid) {
                $children = IndustryCateModel::getIndustryCate(['pid' => $v['id']]);
                // 不存在二级分类，删除当前一级分类
                if (!$children) {
                    unset($data[$k]);
                }
            }
        }

        return $data;
    }

    /**
     * 指定行业分类
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findIndustryCate($where)
    {
        return IndustryCateModel::findIndustryCate($where);
    }

    /**
     * 行业分类指定值
     * @param $where
     * @param string $field
     * @return mixed
     */
    public static function findIndustryCateValue($where, $field = 'name')
    {
        return IndustryCateModel::findIndustryCateValue($where, $field);
    }

    /**
     * 新增行业分类
     * @param $where
     * @return mixed
     */
    public static function createIndustryCate($where)
    {
        return IndustryCateModel::createIndustryCate($where);
    }
}